package be.phl.mycinema;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import be.phl.mycinema.adapters.MyListViewAdapter;
import be.phl.mycinema.classes.Film;
import be.phl.mycinema.classes.ReadJsonTask;

import com.google.gson.reflect.TypeToken;

public class SearchActivity extends Activity implements OnItemClickListener {

	private static final String TAG = "SEARCH";
	private ArrayList<Film> films;
	private ListView myListView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		myListView = (ListView) findViewById(R.id.search_listView);
		
		try {
			if (Utils.isWebserviceAvailable()) {
				ReadJsonTask<Film> a = new ReadJsonTask<Film>(this, TAG, new TypeToken<Collection<Film>>() {}.getType());
				films = a.execute(Utils.WEBSERVICE_BASE_ADDRESS + "/film/search/" + getIntent().getStringExtra("SEARCHVALUE")).get();
				
				if(films.size() == 0) {
					Toast.makeText(this, "Geen film gevonden met die titel !", Toast.LENGTH_LONG).show();
				} else {				
					myListView.setAdapter(new MyListViewAdapter(films, this));	
					myListView.setOnItemClickListener(this);
				}
			} else {
				Toast.makeText(getBaseContext(), "De server is niet bereikbaar, probeer later opnieuw.", Toast.LENGTH_LONG).show();
				finish();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}				
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
		Intent i = new Intent(this, DetailActivity.class);
    	i.putExtra("FILM", films.get(arg2));
    	startActivity(i);
    	
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent parentActivityIntent = new Intent(this,MainActivity.class);
			parentActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(parentActivityIntent);
			finish();
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		if (! Utils.isNetworkAvailable((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))) {
			Toast.makeText(getBaseContext(), "Er is geen internetverbinding beschikbaar!", Toast.LENGTH_LONG).show();
			finish();
		} else if (! Utils.isWebserviceAvailable()) {
			Toast.makeText(getBaseContext(), "De server is niet bereikbaar, probeer later opnieuw.", Toast.LENGTH_LONG).show();
			finish();
		}
	}
	
    @Override
    public void onBackPressed() {
    	// Back button disabled
    }
}
